<template>
  <div>
    <el-dialog title="新增用户" :visible.sync="dialogVisible" :before-close="close" width="600px">
      <el-form style="padding-right: 45px;" :model="userModel" :rules="rules" ref="userModel" label-width="100px" class="demo-userModel">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userModel.username"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realname">
          <el-input v-model="userModel.realname"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="userModel.password"></el-input>
        </el-form-item>
        <el-form-item label="角色分配" prop="roles">
          <el-checkbox-group v-model="userModel.roles">
            <el-checkbox v-for="(item,index) in roleList" :key="index" name="Roles" :label="item.id">{{item.name}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="区选择" >
        	<el-select v-model="userModel.regionid" placeholder="请选择区" @change="getStreetIdSelect">
        		<el-option v-for="item in editRegionIdSelect" :key="item.id" :label="item.name" :value="item.id">
        		</el-option>
        	</el-select>
        </el-form-item>
        <el-form-item label="街道选择" >
        	<el-select v-model="userModel.streetid" placeholder="请选择街道" @change="getNeighborhoodCommitteeIdSelect">
        		<el-option v-for="item in editStreetIdSelect" :key="item.id" :label="item.streetname" :value="item.id">
        		</el-option>
        	</el-select>
        </el-form-item>
        <el-form-item label="居委选择" >
        	<el-select v-model="userModel.neighborhoodcommitteeid" placeholder="请选择居委">
        		<el-option v-for="item in editNeighborhoodCommitteeIdSelect" :key="item.id" :label="item.name" :value="item.id">
        		</el-option>
        	</el-select>
        </el-form-item>
      </el-form>
      <div class="dialog-footer text-center mt-3">
        <el-button type="primary" @click="submitForm('userModel')">确认</el-button>
        <el-button @click="close">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {GetRegionPaged,getJuWeis,getStreePaged} from '@/api/address.js'
export default {
  props: ["dialogVisible"], //接受父组件的值
  data() {
    return {
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        realname: [
          { required: true, message: "请输入真实姓名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        roles: [{ required: true, message: "请选择角色", trigger: "blur" }]
      },
      userModel: {
        username: "",
        realname: "",
        password: "",
        registertype: 1,
        roles: [],
        regionId: "",
        streetId: "",
        neighborhoodcommitteeid: ""
      },
      editRegionIdSelect: [],
      editStreetIdSelect: [],
      editNeighborhoodCommitteeIdSelect: [],
      roleList: [],
      visible: false
    };
  },

  created() {
    this.getRoles();
    this.getRegionIdSelect();
  },

  methods: {
    getRoles: function() {
      this.$api.get("/api/Role/getRoleAll", null).then(rsp => {
        if (rsp != false) {
          this.roleList = rsp.data;
        }
      });
    },

    submitForm: function(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$api.post("/api/User/register", this.userModel,true).then(rsp => {
            if (rsp != false) {
              this.close();
              this.$emit("successAddUser");
            }
          });
        }
      });
    },
    getRegionIdSelect(){
      GetRegionPaged({pageIndex:1,pageSize:999}).then(res=>{
        this.editRegionIdSelect = res.data.list
      })
    },
    getStreetIdSelect(){
      getStreePaged({pageIndex:1,pageSize:999,RegionId:this.userModel.regionid}).then(res=>{
        this.editStreetIdSelect = res.data.list
      })

    },
    getNeighborhoodCommitteeIdSelect(){
      getJuWeis({pageIndex:1,pageSize:999,streetid:this.userModel.streetid}).then(res=>{
        this.editNeighborhoodCommitteeIdSelect = res.data.list
      })
    },

    close() {
      this.$emit("closeAddUser");
    }
  }
};
</script>
